//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.ruoyi.system.excel;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.handler.SheetWriteHandler;
import com.alibaba.excel.write.merge.AbstractMergeStrategy;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.List;

public class ExcelUtils {
    public ExcelUtils() {
    }



    public static <T> void write(HttpServletResponse response, String filename, String sheetName, Class<T> head, List<T> data) throws IOException {
        response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
        response.setContentType("application/vnd.ms-excel;charset=UTF-8");
        ((ExcelWriterBuilder)((ExcelWriterBuilder)EasyExcel.write(response.getOutputStream(), head).autoCloseStream(false).registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()))).sheet(sheetName).doWrite(data);
    }

    public static <T> List<T> read(MultipartFile file, Class<T> head) throws IOException {
        return ((ExcelReaderBuilder)EasyExcel.read(file.getInputStream(), head, (ReadListener)null).autoCloseStream(false)).doReadAllSync();
    }

    public static <T> List<T> read(MultipartFile file, Class<T> head, ReadListener<T> listener) throws IOException {
        return ((ExcelReaderBuilder)EasyExcel.read(file.getInputStream(), head, listener).autoCloseStream(false)).doReadAllSync();
    }

    public static <T> void write(HttpServletResponse response, String filename, String sheetName, Class<T> head, List<T> data, SheetWriteHandler sheetWriteHandler) throws IOException {
        response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
        response.setContentType("application/vnd.ms-excel;charset=UTF-8");
        ((ExcelWriterBuilder)((ExcelWriterBuilder)((ExcelWriterBuilder)EasyExcel.write(response.getOutputStream(), head).autoCloseStream(false).registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()))).registerWriteHandler(sheetWriteHandler)).sheet(sheetName).doWrite(data);
    }

    public static <T> void write(HttpServletResponse response, String filename, String sheetName, Class<T> head, List<T> data, AbstractMergeStrategy strategy) throws IOException {
        response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
        response.setContentType("application/vnd.ms-excel;charset=UTF-8");
        ((ExcelWriterBuilder)((ExcelWriterBuilder)((ExcelWriterBuilder)EasyExcel.write(response.getOutputStream(), head).autoCloseStream(false).registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()))).registerWriteHandler(strategy)).sheet(sheetName).doWrite(data);
    }

    public static <T> void writeTemplate(HttpServletResponse response, String filename, String templatePath, Object data) throws IOException {
        defaultHeader(response, filename);
        WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
        contentWriteCellStyle.setWrapped(true);
        InputStream resourceAsStream = ExcelUtils.class.getClassLoader().getResourceAsStream(templatePath);
        ((ExcelWriterBuilder)EasyExcel.write(response.getOutputStream()).registerWriteHandler(new HorizontalCellStyleStrategy((WriteCellStyle)null, contentWriteCellStyle))).withTemplate(resourceAsStream).sheet().doFill(data);
    }

    public static void defaultHeader(HttpServletResponse response, String filename) throws IOException {
        response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
        response.setContentType("application/vnd.ms-excel;charset=UTF-8");
    }

    public static String getCellName(int index) {
        StringBuilder name;
        for(name = new StringBuilder(); index >= 0; index = index / 26 - 1) {
            int i = index % 26;
            name.append((char)(i + 65));
        }

        return name.reverse().toString();
    }

}
